package com.itboye.qihang.volley;

import android.util.Log;

import com.android.volley.ServerError;
import com.android.volley.VolleyError;


public class BaseErrorListener implements XErrorListener {

    private ICompleteListener listener;

    public BaseErrorListener(ICompleteListener listener) {
        this.listener = listener;
    }

    @Override
    public void onErrorResponse(Exception exception, String code, String msg) {
//			Log.v("error", exception.getMessage());
        Log.d("UnLoginState", code);
        if (listener != null) {

            if (code == "0") {
                code = "-199";

            } else if (code.equals("-1")) {
//                LoginController.setLoginState(new UnLoginState());
//                exit();
//                Intent intent = new Intent(FilterUtil.LOGIN);
//                MyApplication.getInstance().sendBroadcast(intent);
                ResultEntity resultEntity = new ResultEntity(code, msg, exception);
                listener.failure(resultEntity);
                return;

            }
            if (msg == null || msg.isEmpty()) {
                if (exception != null) {
                    msg = exception.getMessage();
                } else {
                    msg = "发生未知错误!";
                }
            }
            if (exception instanceof ServerError) {
                ServerError error = (ServerError) exception;
                code = "" + error.networkResponse.statusCode;
                msg = "服务器返回错误!";

            }

            if (exception instanceof VolleyError) {
                VolleyError jsonException = (VolleyError) exception;
                String error = jsonException.getLocalizedMessage();
                Log.d("exception==",exception.getMessage());
                msg = "数据解析有误";
            }
            ResultEntity result = new ResultEntity(code, msg, exception);
            listener.failure(result);
        }
    }

    private void exit() {
        // TODO Auto-generated method stub
        Log.d("werwerwewerwe", "hahhaha");
//        LoginController.setLoginState(new UnLoginState());
//
//        SPUtils.put(MyApplication.getInstance(), null, Const.USERNAME, "");
////		SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD, "");
//        SPUtils.put(MyApplication.getInstance(), null, Const.NICK, "");
//        SPUtils.put(MyApplication.getInstance(), null, Const.UID, "");
//        SPUtils.put(MyApplication.getInstance(), null, Const.SEX, "");
//        SPUtils.put(MyApplication.getInstance(), null, Const.S_ID, "");
//        SPUtils.put(MyApplication.getInstance(), null, Const.HEAD, "");

    }
}

